package 数据结构与算法.实验一;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Random;

public class findWord {
    /* 1写入文件
      2输入单词 比较2分
      3怎么把英语单词读取出来，怎么比较
     */
    public static void main(String[] args) {
        int m=0;
        String [] words=new String[10000000];
        try {
            BufferedReader br=new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream("D:/txt/Words.txt")
                    )
            );
            String linestr;
            while((linestr=br.readLine())!=null){
                //System.out.println(linestr);
                words[m++]=linestr;
            }
            br.close();
        }catch(Exception e){
            System.out.println("文件操作失败");
            e.printStackTrace();
        }
        Random random=new Random();
        int o=random.nextInt(103975)+1;
        System.out.println(o);
        String []ss=new String[103936];
        for(int p=0;p<103936;p++){
            String []u=words[p].split(",");
            ss[p]=u[0];
        }
        int e=103936/2;
        int d=2;
        while(true){
            int f=103936/(2*d);
            int a=compare(ss[o],ss[e]);
            if(a==1){
                e=e+f;
            }else if(a==-1){
                e=e-f;
            }else if(a==0){
                System.out.println("结果是\n"+words[e]);
                System.out.println(words[o]);
                break;
            }
            d=d*2;
        }
    }
    public static int compare(String i,String r){
        char[] chars1=i.toCharArray();
        char[] chars2=r.toCharArray();
        int d=0;
        int e=0;
        int u=0;
        if(chars1.length>=chars2.length){
            d=chars1.length-2;
            u=chars2.length-2;
        }else{
            d=chars2.length-2;
            u=chars1.length-2;
        }
        for(int j=0;j<u;j++){
            if(chars1[j]>chars2[j]){
                e=1;
                break;
            }else  if(chars1[j]<chars2[j]){
                e=-1;
                break;
            }else{
                e=0;
            }
        }
        return e;
    }

}
